/*
 * OutputFrame.java
 *
 * Created on June 5, 2006, 10:27 AM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

/**
 *
 * @author Timoz
 */
import javax.swing.*;
import java.util.*;
import java.awt.*;
import javax.print.*;
import java.awt.print.*;

public class OutputFrame extends JFrame {
    
    MainPanel mainPanel;
    
    /** Creates a new instance of OutputFrame */
    public OutputFrame (MainPanel mainPanel) {
        initComponents();
        this.mainPanel=mainPanel;
        add(mainPanel);
        if (mainPanel.panel.getWidth() > 420) {
            setSize(mainPanel.panel.getWidth(),mainPanel.panel.getHeight()+100);
        } else {
            setSize(420,mainPanel.panel.getHeight()+100);
        }
        setResizable(false);
        Toolkit kit = Toolkit.getDefaultToolkit();
        Dimension dimension = kit.getScreenSize();
        int width = (int)dimension.getWidth();
        int height = (int)dimension.getHeight();
        if (getWidth()>width && getHeight() > height) {
            setSize(width,height);
        } else if (getWidth() > width-50) {
            setSize(width-50,getHeight());
        } else if (getHeight() > height-100) {
            setSize(getWidth(),height-100);
        }
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    }
    
    public void refreshScrolls() {
        if (mainPanel.panel.getWidth()>getWidth()) {
            mainPanel.scrollHorizontal.setVisible(true);
            mainPanel.scrollHorizontal.setMaximum(mainPanel.panel.getWidth()-getWidth());
        }
        if (mainPanel.panel.getHeight()>getHeight()) {
            mainPanel.scrollVertical.setVisible(true);
            mainPanel.scrollVertical.setMaximum(mainPanel.panel.getHeight()-getHeight());
        }
    }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">
    private void initComponents() {
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();
        jMenuItem2 = new javax.swing.JMenuItem();
        jSeparator1 = new javax.swing.JSeparator();
        jMenuItem3 = new javax.swing.JMenuItem();
        jSeparator2 = new javax.swing.JSeparator();
        jMenuItem5 = new javax.swing.JMenuItem();
        jMenuItem4 = new javax.swing.JMenuItem();
        jSeparator3 = new javax.swing.JSeparator();
        jMenuItem6 = new javax.swing.JMenuItem();
        jMenu2 = new javax.swing.JMenu();
        jMenuItem7 = new javax.swing.JMenuItem();
        jMenuItem8 = new javax.swing.JMenuItem();
        jMenuItem9 = new javax.swing.JMenuItem();
        jSeparator4 = new javax.swing.JSeparator();
        jMenuItem10 = new javax.swing.JMenuItem();
        jMenuItem11 = new javax.swing.JMenuItem();
        jMenu3 = new javax.swing.JMenu();
        jMenuItem12 = new javax.swing.JMenuItem();
        jMenuItem13 = new javax.swing.JMenuItem();
        jMenuItem14 = new javax.swing.JMenuItem();
        jMenuItem15 = new javax.swing.JMenuItem();
        jMenuItem16 = new javax.swing.JMenuItem();
        jMenu4 = new javax.swing.JMenu();
        jMenuItem17 = new javax.swing.JMenuItem();
        jMenuItem18 = new javax.swing.JMenuItem();
        jSeparator5 = new javax.swing.JSeparator();

        jMenu1.setMnemonic('F');
        jMenu1.setText("File");
        jMenuItem1.setMnemonic('S');
        jMenuItem1.setText("Save");
        jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem1ActionPerformed(evt);
            }
        });

        jMenu1.add(jMenuItem1);

        jMenuItem2.setMnemonic('A');
        jMenuItem2.setText("Save As");
        jMenuItem2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem2ActionPerformed(evt);
            }
        });

        jMenu1.add(jMenuItem2);

        jMenu1.add(jSeparator1);

        jMenuItem3.setMnemonic('E');
        jMenuItem3.setText("Export");
        jMenuItem3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem3ActionPerformed(evt);
            }
        });

        jMenu1.add(jMenuItem3);

        jMenu1.add(jSeparator2);

        jMenuItem5.setMnemonic('v');
        jMenuItem5.setText("Print Preview");
        jMenuItem5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem5ActionPerformed(evt);
            }
        });

        jMenu1.add(jMenuItem5);

        jMenuItem4.setMnemonic('P');
        jMenuItem4.setText("Print");
        jMenuItem4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem4ActionPerformed(evt);
            }
        });

        jMenu1.add(jMenuItem4);

        jMenu1.add(jSeparator3);
        
        jMenuItem6.setMnemonic('x');
        jMenuItem6.setText("Exit");
        jMenuItem6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem6ActionPerformed(evt);
            }
        });

        jMenu1.add(jMenuItem6);

        jMenuBar1.add(jMenu1);

        jMenu2.setMnemonic('E');
        jMenu2.setText("Edit");
        jMenuItem18.setMnemonic('U');
        jMenuItem18.setText("Undo");
        jMenuItem18.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem18ActionPerformed(evt);
            }
        });

        jMenu2.add(jMenuItem18);        
        
        jMenu2.add(jSeparator5);
        
        jMenuItem7.setMnemonic('C');
        jMenuItem7.setText("Copy");
        jMenuItem7.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem7ActionPerformed(evt);
            }
        });

        jMenu2.add(jMenuItem7);

        jMenuItem8.setMnemonic('t');
        jMenuItem8.setText("Cut");
        jMenuItem8.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem8ActionPerformed(evt);
            }
        });

        jMenu2.add(jMenuItem8);

        jMenuItem9.setMnemonic('P');
        jMenuItem9.setText("Paste");
        jMenuItem9.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem9ActionPerformed(evt);
            }
        });

        jMenu2.add(jMenuItem9);

        jMenu2.add(jSeparator4);

        jMenuItem10.setMnemonic('D');
        jMenuItem10.setText("Delete Record");
        jMenuItem10.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem10ActionPerformed(evt);
            }
        });

        jMenu2.add(jMenuItem10);

        jMenuItem11.setMnemonic('F');
        jMenuItem11.setText("Find Record");
        jMenuItem11.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem11ActionPerformed(evt);
            }
        });

        jMenu2.add(jMenuItem11);

        jMenu3.setMnemonic('G');
        jMenu3.setText("Go to");
        jMenuItem12.setMnemonic('F');
        jMenuItem12.setText("|<< First");
        jMenuItem12.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem12ActionPerformed(evt);
            }
        });

        jMenu3.add(jMenuItem12);

        jMenuItem13.setMnemonic('L');
        jMenuItem13.setText(">>| Last");
        jMenuItem13.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem13ActionPerformed(evt);
            }
        });

        jMenu3.add(jMenuItem13);

        jMenuItem14.setMnemonic('N');
        jMenuItem14.setText(">>  Next");
        jMenuItem14.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem14ActionPerformed(evt);
            }
        });

        jMenu3.add(jMenuItem14);

        jMenuItem15.setMnemonic('P');
        jMenuItem15.setText("<<  Previous");
        jMenuItem15.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem15ActionPerformed(evt);
            }
        });

        jMenu3.add(jMenuItem15);

        jMenuItem16.setMnemonic('w');
        jMenuItem16.setText(">>* New Record");
        jMenuItem16.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem16ActionPerformed(evt);
            }
        });

        jMenu3.add(jMenuItem16);

        jMenu2.add(jMenu3);

        jMenuBar1.add(jMenu2);

        jMenu4.setMnemonic('H');
        jMenu4.setText("Help");
        jMenuItem17.setMnemonic('A');
        jMenuItem17.setText("About");
        jMenuItem17.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem17ActionPerformed(evt);
            }
        });

        jMenu4.add(jMenuItem17);

        jMenuBar1.add(jMenu4);

        setJMenuBar(jMenuBar1);

        /*org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 279, Short.MAX_VALUE)
        );
         */
    }// </editor-fold>

    private void jMenuItem17ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
        new About();
    }

    private void jMenuItem16ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
        mainPanel.outputPanel.navigateRecords(">>*");
    }

    private void jMenuItem15ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
        mainPanel.outputPanel.navigateRecords("<<");
    }

    private void jMenuItem14ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
        mainPanel.outputPanel.navigateRecords(">>");
    }

    private void jMenuItem13ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
        mainPanel.outputPanel.navigateRecords(">>|");
    }

    private void jMenuItem12ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
        mainPanel.outputPanel.navigateRecords("|<<");
    }
    
    private void jMenuItem18ActionPerformed(java.awt.event.ActionEvent evt) {
        mainPanel.outputPanel.undoRecord();
    }

    private void jMenuItem11ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
        // Find record action                                
        String[] fields = mainPanel.outputPanel.mainFields.split("\\,");
        mainPanel.outputPanel.new FindRecordFrame(fields).setVisible(true);        
    }

    private void jMenuItem10ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
        // Delete record action
        String msg = "You are about to delete 1 record.\n\nIf you click Yes, you wont be able to undo this delete operation.\nAre you sure you want to delete this record?";
        if(JOptionPane.showConfirmDialog(null, msg,"Confirm delete", JOptionPane.YES_NO_OPTION)==JOptionPane.OK_OPTION){
            mainPanel.outputPanel.deleteRecord();
        } 
    }

    private void jMenuItem9ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:        
        mainPanel.outputPanel.selectedTextField.paste();
    }

    private void jMenuItem8ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
        mainPanel.outputPanel.selectedTextField.cut();
    }

    private void jMenuItem7ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:        
        mainPanel.outputPanel.selectedTextField.copy();
    }

    private void jMenuItem6ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
        mainPanel.outputPanel.updateRecord("");
        dispose();
    }

    private void jMenuItem4ActionPerformed(java.awt.event.ActionEvent evt) {
         boolean flagPrint=false;
         PrinterJob printerJob=PrinterJob.getPrinterJob();
         flagPrint=printerJob.printDialog();
         if (flagPrint){
             MyFileChooser myFileChooser=new MyFileChooser();
             myFileChooser.printFileChooserSelection=myFileChooser.jFileChooser1.showSaveDialog(null);
         }
// TODO add your handling code here:
    }

    private void jMenuItem5ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
    }

    private void jMenuItem3ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
    }

    private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
    }

    private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {                                           
// TODO add your handling code here:
    }                                          
    
    // Variables declaration - do not modify
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenu jMenu3;
    private javax.swing.JMenu jMenu4;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JMenuItem jMenuItem10;
    private javax.swing.JMenuItem jMenuItem11;
    private javax.swing.JMenuItem jMenuItem12;
    private javax.swing.JMenuItem jMenuItem13;
    private javax.swing.JMenuItem jMenuItem14;
    private javax.swing.JMenuItem jMenuItem15;
    private javax.swing.JMenuItem jMenuItem16;
    private javax.swing.JMenuItem jMenuItem17;
    private javax.swing.JMenuItem jMenuItem18;
    private javax.swing.JMenuItem jMenuItem2;
    private javax.swing.JMenuItem jMenuItem3;
    private javax.swing.JMenuItem jMenuItem4;
    private javax.swing.JMenuItem jMenuItem5;
    private javax.swing.JMenuItem jMenuItem6;
    private javax.swing.JMenuItem jMenuItem7;
    private javax.swing.JMenuItem jMenuItem8;
    private javax.swing.JMenuItem jMenuItem9;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JSeparator jSeparator3;
    private javax.swing.JSeparator jSeparator4;
    private javax.swing.JSeparator jSeparator5;
    // End of variables declaration     
}
